package project.AbM;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListBlogActivity extends ListActivity{
   
    private ProgressDialog b_ProgressDialog = null;
    private ArrayList<Blog> l_blogs = null;
    private BlogAdapter b_adapter;
    private Runnable viewOrders;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_blog);
        l_blogs = new ArrayList<Blog>();
        this.b_adapter = new BlogAdapter(this, R.layout.blog_item, l_blogs);
        setListAdapter(this.b_adapter);
       
        viewOrders = new Runnable(){
            @Override
            public void run() {
                getOrders();
            }
        };
        Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
        thread.start();
        b_ProgressDialog = ProgressDialog.show(ListBlogActivity.this,"Veuillez patienter...","Récupération des données en cours...", true);
    }
    private Runnable returnRes = new Runnable() {

        @Override
        public void run() {
            if(l_blogs != null && l_blogs.size() > 0){
                b_adapter.notifyDataSetChanged();
                for(int i=0;i<l_blogs.size();i++)
                b_adapter.add(l_blogs.get(i));
            }
            b_ProgressDialog.dismiss();
            b_adapter.notifyDataSetChanged();
        }
    };
    private void getOrders(){
          try{
        	  l_blogs = new ArrayList<Blog>();
        	  
              Blog o1 = new Blog();
              o1.setBlogName("Nouveau");
              o1.setBlogDesc("Ajouter un nouveau blog en favori");
              o1.setBlogImage(R.drawable.add);
              
              Blog o2 = new Blog();
              o2.setBlogName("Mon premier blog");
              o2.setBlogDesc("Plateforme Wordpress");
              o2.setBlogImage(R.drawable.wordpress);
              
              Blog o3 = new Blog();
              o3.setBlogName("Mon deuxième blog");
              o3.setBlogDesc("Plateforme DotClear");
              o3.setBlogImage(R.drawable.dotclear);
              
              l_blogs.add(o1);
              l_blogs.add(o2);
              l_blogs.add(o3);
              
              Thread.sleep(5000);
              Log.i("ARRAY", ""+ l_blogs.size());
            } catch (Exception e) {
              Log.e("BACKGROUND_PROC", e.getMessage());
            }
            runOnUiThread(returnRes);
        }
    private class BlogAdapter extends ArrayAdapter<Blog> {

        private ArrayList<Blog> items;

        public BlogAdapter(Context context, int textViewResourceId, ArrayList<Blog> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.blog_item, null);
                }
                Blog b = items.get(position);
                if (b != null) {
                        TextView blog_title = (TextView) v.findViewById(R.id.blog_title);
                        TextView blog_desc = (TextView) v.findViewById(R.id.blog_desc);
                        ImageView blog_image = (ImageView) v.findViewById(R.id.blog_image);
                        if (blog_title != null) {
                        	blog_title.setText(b.getBlogName());                            }
                        if(blog_desc != null){
                        	blog_desc.setText(b.getBlogImage());
                        }
                        if(blog_image != null ){
                        	blog_image.setImageResource(b.getBlogImage());
                        }
                }
                return v;
        }
}
}